<template>
  <div>
    <el-form :model="formData" label-width="90px" :rules="FormRules" ref="addForm">
      <el-form-item label="集团" prop="groupid">
        <el-select v-model="formData.groupid" placeholder="请选择集团" @change="changeGroup">
          <el-option
            v-for="item in groupList"
            :key="item.pkCorp"
            :label="item.unitname"
            :value="item.pkCorp"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="公司" prop="corptypeid">
        <el-select v-model="formData.corptypeid" placeholder="请选择公司" @change="getCorpList">
          <el-option
            v-for="item in corptypeList"
            :key="item.pkCorp"
            :label="item.unitname"
            :value="item.pkCorp"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="分公司" prop="pkCorp">
        <el-select v-model="formData.pkCorp" placeholder="请选择分公司">
          <el-option
            v-for="item in corpList"
            :key="item.pkCorp"
            :label="item.unitname"
            :value="item.pkCorp"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工" prop="cuserid">
        <el-select v-model="formData.cuserid" placeholder="请选择员工" filterable>
          <el-option
            v-for="item in userArr"
            :key="item.cuserid"
            :label="item.userName"
            :value="item.cuserid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handSave()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  //   menuAdd,
  //   menuEdit,
  //   menuList,
  //   selectMenuFSTreeList,
  wlcPurchasingManagerAdd,
  wlcPurchasingManagerEdit,
  omJobSelectGroup,
  omJobSelectCorpType,
  smUserList,
  omJobSelectBdCorp,
} from "../../../api/baseSetting";
export default {
  data() {
    return {
      formData: {
        groupid: "",
        groupname: "",
        corptypeid: "",
        corptypename: "",
        cuserid: "",
        pkCorp: "",
        psnname: "",
      },
      FormRules: {
        groupid: [{ required: true, message: "请选择集团", trigger: "blur" }],
        corptypeid: [
          { required: true, message: "请选择公司", trigger: "blur" },
        ],
        pkCorp: [{ required: true, message: "请选择分公司", trigger: "blur" }],
        cuserid: [{ required: true, message: "请选择员工", trigger: "blur" }],
      },
      options: [], //父级菜单
      groupList: [], //集团列表
      corptypeList: [], //公司列表
      corpList: [], //子公司列表
      userArr: [], //员工列表
    };
  },
  props: {
    sendFormData: {
      type: Object,
      default: {},
    },
  },
  methods: {
    //点击保存
    handSave() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          var formData = this.formData;
          let objName = {
            groupname: this.getValue(
              this.groupList,
              "pkCorp",
              this.formData.groupid
            ).unitname,
            corptypename: this.getValue(
              this.corptypeList,
              "pkCorp",
              this.formData.corptypeid
            ).unitname,
            unitname: this.getValue(
              this.corpList,
              "pkCorp",
              this.formData.pkCorp
            ).unitname,
            psnname: this.getValue(
              this.userArr,
              "cuserid",
              this.formData.cuserid
            ).userName,
          };
          let data = Object.assign(
            {},
            this.sendFormData,
            this.formData,
            objName
          );
          if (this.sendFormData.id) {
            //编辑
            wlcPurchasingManagerEdit(data).then((res) => {
              this.$message({
                message: "成功",
                type: "success",
              });
              this.$emit("hideTotas");
            });
          } else {
            //新增
            wlcPurchasingManagerAdd(data).then((res) => {
              this.$message({
                message: "成功",
                type: "success",
              });
              this.$emit("hideTotas");
            });
          }
        }
      });
    },
    /**获取数组对于字段的值
     * list 数组
     * key 数组对应字段
     * value  要获取到对应字段等于这个值的数据
     */
    getValue(list, key, value, valueKye) {
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        if (item[key] == value) {
          return item;
        }
      }
    },

    //选择集团
    changeGroup(e) {
      this.getCorptypeList(e);
    },
    //获取集团列表
    getGroupList() {
      omJobSelectGroup({}).then((res) => {
        this.groupList = res;
      });
    },
    //获取公司列表
    getCorptypeList(e) {
      omJobSelectCorpType({ pkCorp: e }).then((res) => {
        this.corptypeList = res;
      });
    },
    //获取分公司列表
    getCorpList(e) {
      omJobSelectBdCorp({ pkCorp: e }).then((res) => {
        this.corpList = res;
      });
    },
    //获取员工列表
    getStaffList() {
      smUserList({
        page: {
          page: 1,
          pageSize: 100000,
        },
      }).then((res) => {
        this.userArr = res.records;
      });
    },
  },
  created() {
    this.getGroupList();
    this.getStaffList();
  },
  mounted() {
    if (this.sendFormData.id) {
      var data = this.sendFormData;
      this.formData = data;
      if (this.sendFormData.groupid) {
        this.getCorptypeList(this.sendFormData.groupid);
      }
      if (this.sendFormData.corptypeid) {
        this.getCorpList(this.sendFormData.corptypeid);
      }
    } else {
      for (var i in this.formData) {
        this.formData[i] = "";
      }
    }
  },
};
</script>

<style lang="less" scoped>
</style>
